<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
function copy_dir($src, $dest, $justDest)
{
	if( !is_dir($src) ) //then just try copying it
	{
		if (!copy($src, $dest.$justDest))
		{
			return -1;
		}
		return 0;
	}
	if($justDest != NULL)
	{
		//sure src is a directory..
		$dest .= $justDest."/";
		if(!is_dir($dest))
		{
			mkdir($dest);
		}
	}
	
	$d = dir($src);
	while (false !== ($file = $d->read()))
	{
		if (($file{0} == '.') ||
			($file{0} == '..') ||
			($file{0} == '~'))
		{
			continue;
		}
		else if( is_dir($src.$file) )
		{
			if( !is_dir($dest.$file) ) //just check before creatng..
			{
				//echo $dest.":".$dest.$file."\n";
				mkdir($dest.$file);
			}
			$ret = copy_dir($src.$file."/", $dest.$file."/", NULL);
			if( $ret == -1 )
			{
				return -1;
			}
			continue;
		}
		//then it sould be a file..
		if (!copy($src.$file, $dest.$file))
		{
			return -1;
		}
	}
	return 0;	
}

function deldir($dir) {
	$dh=opendir($dir);
	while ($file=readdir($dh)) {
		if($file!="." && $file!="..") 
		{
			$fullpath=$dir."/".$file;
			if(!is_dir($fullpath)) 
			{
				unlink($fullpath);
			} 
			else 
			{
				deldir($fullpath);
			}
		}
	}
	closedir($dh);
	if(rmdir($dir)) 
	{
		return true;
	} 
	else 
	{
		return false;
	}
}

function find_relative_dir($base, $target)
{
	//echo $target;
	if(!is_dir($base))
	{
		$base = dirname($base);
	}
	//echo $base;
	$tok = strtok($base, "/");
	$prefix = "";
	while ($tok)
	{
		if($tok == ".")
		{
   			$prefix .= "./";
		}
		elseif($tok == "..")
		{
			//we cannot do anything here..
			$prefix = "page_designer/";
		}
		else
		{
   			$prefix .= "../";
		}
   		$tok = strtok("/");
	}
	return $prefix.$target;
}

/** this is just copied from semantic_handler */
function copy_include_files($result_dir, $component_dir)
{
	$dir_from = $component_dir."/files_to_copy/";
	if(!is_dir($dir_from))
	{
		return ""; //not specials
	}
	$relative_path = "../store/other/";
	$dir_to = $result_dir;
	
	
	$extensions = array( "js"=> 1, //js files
						 "php"=> 1, //php filesv
						 "ico"=> 1, //icons
						 "jpg"=> 1, //images
						 "css"=> 1, // ''
						 "gif"=> 1, // ''
						 "html"=> 1,
						 "htm"=> 1,
						 "txt"=> 1
						 );
	
	$ret_text = copy_include_dir($dir_from, $result_dir, $relative_path, $extensions, false);
	
	return $ret_text;
}
	
function copy_include_dir($src, $dest, $relative_path, $extensions, $not2include)
{
	$ret_text = "";
	if( !is_dir($src) ) //then just try copying it
	{
		if (!copy($src, $dest))
		{
			return -1;
		}
		return "";
	}
	$d = dir($src);
	while (false !== ($file = $d->read()))
	{
		if (($file{0} == '.') ||
			($file{0} == '..') ||
			($file{0} == '~'))
		{
			continue;
		}
		else if( is_dir($src.$file) )
		{
			if($file == "not2include")
			{
				$not2include = true;
			}
			if( !is_dir($dest.$file) ) //just check before creatng..
			{
				mkdir($dest.$file);
			}
			$ret = copy_include_dir($src.$file."/", $dest.$file."/",
							$relative_path.$file."/", $extensions, $not2include);
			if( $ret == -1 )
			{
				return -1;
			}
			$ret_text .= $ret;
			continue;
		}
		
		$ext = NULL;
		$tok = strtok($file, ".");
		while ($tok)
		{
			$ext = $tok; //to find the last token.
			$tok = strtok(".");
		}
		
		if( $extensions[$ext] != NULL )
		{
			if (!copy($src.$file, $dest.$file))
			{
				//$this->report_error("failed to copy ${php_source}.${$file}...");
				return -1;
			}
			if($not2include == false)
			{
				//now check for special file types..
				if (strtolower($ext) == "js")
				{
					$ret_text .= "<include_js>${relative_path}${file}</include_js>";
				}
				elseif (strtolower($ext) == "css")
				{
					$ret_text .= "<include_css>${relative_path}${file}</include_css>";
				}
			}
		}
	}
	return $ret_text;	
}
?>